﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Extensions
{
	using System.Text;
	using Falcon.Core.Collections;
	using Falcon.Core.Domain.Enums;

	public static class UserSearchExtension
	{
		/// <summary>
		/// Gets the users as text.
		/// </summary>
		/// <param name="users">The users.</param>
		/// <returns></returns>
		public static string AsText(this IPagedList<User, UserSearchSpecification> users)
		{
			if (users.Items.Count <= 0)
			{
				return string.Empty;
			}

			var builder = new StringBuilder();

			for (var i = 0; i < users.Items.Count; i++)
			{
				switch (users.Specification.Scope)
				{
					case UserSearchScope.Name:
						{
							builder.AppendLine(users.Items[i].Name);
						}
						break;
					default:
						{
							builder.AppendLine(users.Items[i].EmailAddress);
						}
						break;
				}
			}

			return builder.ToString();
		}

		/// <summary>
		/// Populates the user search based on scope.
		/// </summary>
		/// <param name="search">The search.</param>
		/// <param name="scope">The scope.</param>
		/// <param name="query">The query.</param>
		public static void QueryBasedOnScope(this UserSearch search, UserSearchScope scope, string query)
		{
			switch (scope)
			{
				case UserSearchScope.Name:
					{
						search.Name = query;
					}
					break;
				default:
					{
						search.EmailAddress = query;
					}
					break;
			}
		}
	}
}